#import nltk
#encoding:utf-8
import requests
import json
class chatbot:
    
    def __init__(self):
        # client_id 为官网获取的AK， client_secret 为官网获取的SK
        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=K74KlCVTweR9V6FZ4dV56hzu&client_secret=TaoLviGh3bN1oGajlqBwR5sl9jGHulGB'
        response = requests.get(host)
        print("chatbot is ready")
        if response:
            resdata = response.json()
            #replace ' to " in order to  turn to json 
            resdata2 = str(resdata)
            resdata3 = resdata2.replace("'",'"')
            jsdata = json.loads(resdata3)
            self.access_token = jsdata['access_token']
            
            
          #baidu robot chat  
    def  saidToAnswer(self,string_said):
        #get taken from baidu in self init to fill it to url then get response 
        access_token = self.access_token
        url = 'https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=' + access_token
        #print(url)

        #string_said = '笑话'
        #string_said = stringsaid
        string_data = '"query\":\"{}\",\"user_id\":\"88888\"'.format(string_said)
        post_data0 = "{\"log_id\":\"UNITTEST_10000\",\"version\":\"2.0\",\"service_id\":\"S26623\",\"session_id\":\"\",\"request\":{\"query\":\"hello\",\"user_id\":\"88888\"},\"dialog_state\":{\"contexts\":{\"SYS_REMEMBERED_SKILLS\":[\"1057\"]}}}".replace('"query\":\"hello\",\"user_id\":\"88888\"',string_data)
        #print(post_data0)

        post_data = post_data0.encode('UTF-8')
        headers = {'content-type': 'application/x-www-form-urlencoded'}
        response = requests.post(url, data=post_data, headers=headers)
        if response:
            resdata = response.json()
            print(resdata)
            #resanswer = resdata['result']['response_list'][1]['action_list'][0]['say']
            resanswer = resdata['result']['response_list'][0]['action_list'][0]['say']
            return resanswer
            #jsdata = str(resdata)
            #print(resdata)
    
    def saidBySkill(self,string_said,bot_id):
        access_token = self.access_token
        url = 'https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token=' + access_token
        string_data = '"query\":\"{}\"'.format(string_said)
        post_data0 = "{\"bot_session\":\"\",\"log_id\":\"7758521\",\"request\":{\"bernard_level\":1,\"client_session\":\"{\\\"client_results\\\":\\\"\\\", \\\"candidate_options\\\":[]}\",\"query\":\"你好\",\"query_info\":{\"asr_candidates\":[],\"source\":\"KEYBOARD\",\"type\":\"TEXT\"},\"updates\":\"\",\"user_id\":\"88888\"},\"bot_id\":\"1015544\",\"version\":\"2.0\"}".replace('"query\":\"你好\"',string_data)
        post_data1 = post_data0.replace('1015544',bot_id)
        print(post_data1)
        post_data = post_data1.encode('UTF-8')
        headers = {'content-type': 'application/x-www-form-urlencoded'}
        response = requests.post(url, data=post_data, headers=headers)
        if response:
            print (response.json())
            resdata = response.json()
            resanswer = resdata['result']['response']['action_list'][2]['say']
            #print(resanswer)
            return resanswer
    #function not complete and need continu when having time
  